<?php
class Ring_sizer extends MY_Front_Controller {

	function __construct() {
		parent::__construct();
	}
	
    function index()
    {
        $this->load->helper('form');
        $this->load->library ( 'form_validation' );
        $this->form_validation->set_rules ( 'name', lang('name'), 'required' );
        $this->form_validation->set_rules ( 'email', lang('email'), 'required|email' );

        $this->template->add_js('application/assets/js/ring_sizer.js');
        $this->template->add_js('application/assets/js/jqueryPlugins/jquery.validate.js');
            	
        $data = array();
        if ($this->form_validation->run () == FALSE) {
	        $this->template->write_view ( 'content', 'front/ring_sizer/ring_sizer.php', $data );
	        $this->template->render ();
        }

    }
    public function send_form()
    {
        $this->load->library('email');
        $response = array('status'=>1,'message'=>'');
        $data = array();
        $nume = ($this->input->post('nume'))?$this->input->post('nume'):'';
        $email = ($this->input->post('email'))?$this->input->post('email'):'';
        $mesaj = ($this->input->post('mesaj'))?$this->input->post('mesaj'):'';

        $data['nume']  = $nume;
        $data['email'] = $email;
        $data['mesaj'] = $mesaj;
        
        $this->load->model('user_model');
        
        $adresses = $this->user_model->getEmailAdresses($this->config->item('lang_obj')->id);
              
        $send_to = '';
        if (!empty($adresses)){
        	foreach ($adresses as $addr){
        		$send_to .= $addr->user_email . ',';
        	}
        }
        
        if (strlen($send_to)){
        	$send_to = substr($send_to, 0, -1);
        }

        $message = $this->load->view('front/ring_sizer/ring_sizer_form.php',$data,true);
        $this->email->from(htmlentities(strip_tags($email)), '' );
        $this->email->to($send_to);
        $this->email->bcc($this->config->item('bcc_email'));
        $this->email->mailtype = 'html';
        $this->email->subject(lang('ring_sizer_form'));
        $this->email->message($message);
        if ($this->email->send()){
        		$response['status'] = 1;
        }
        else{
                $response['status'] = 0;
        }
        echo json_encode($response);
    }
}